import {service as config} from './config';
/*
 * service API here
 *
 *
 * */

export const service = {
    getDetail(id, callback) {
        wxapi.$get(config.getDetail.format(id), '', callback);
    },
    getList(param, callback) {
        wxapi.$post(config.getList, param, callback);
    },
    getCriteria(callback) {
        wxapi.$get(config.getCriteria, '', callback);
    },
    getHotkeywords(shelfid, callback) {
        wxapi.$post(config.getHotkeywords, '', callback);
    },
    makeanappointment(param, callback) {
      wxapi.$post(config.makeanappointment, param, callback);
    }
};

/*
* weixin API here
*
*
* */

export const wxapi = {
    $get(...args) {
        this.request(['GET', ...args]);
    },
    $post(...args) {
        this.request(['POST', ...args]);
    },
    request(args) {
        wx.request({
            url: args[1],
            data: args[2],
            method: args[0],
            success: function (res) {
                args[3] && args[3].call(this, res);
            },
            fail(res){
                args[3] && args[3].call(this, res);
            },
            complete(){}
        })
    },

    login(callback) {
        wx.login({
            success(res) {
                if (res.code) {
                    callback && callback.call(this, res);
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            },
            fail() {

            }
        })
    },
    scan(callback) {
        wx.scanCode({
            success(res) {
                callback && callback.call(this, res);
            }
        })
    },
    getUserInfo(callback) {
        wx.getUserInfo({
            success(res) {
                //errMsg:"getUserInfo:ok"
                callback && callback.call(this, res);
            },
            fail(res) {
                console.log(res.errMsg)
                //errMsg: "getUserInfo:fail auth deny"
            }
        })
    },
    getLocation(callback) {
        var that = this;
        wx.getLocation({
            type: 'wgs84',
            success: function (res) {
                callback && callback.call(this, res);
            },
            fail(res) {
                callback && callback.call(this);
                console.log(res.errMsg)
            }
        })
    }

}
